﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;

/// <summary>
/// CSV解析类
/// </summary>
public class CSVHelper
{
    public const string PATH_NAME = "Data/Table";

    public static string Combine(string csv_name)
    {
        return Path.Combine(PATH_NAME, csv_name);
    }

    public static byte[] GetBytes(string table_path)
    {
        return SettingManager.Instance.GetDataByte(table_path);
    }

    #region 获取唯一键值
    public static ulong GetKey(int key1)
    {
        if (key1 < 0)
        {
            return 0;
        }

        return Convert.ToUInt64(key1);
    }

    public static ulong GetKey(int key1, int key2)
    {
        return (((ulong)key1 & 0xffffffff) | (((ulong)key2 & 0xffffffff) << 32));
    }

    public static ulong GetKey(int key1, int key2, int key3)
    {
        short shortKey2 = Convert.ToInt16(key2);
        short shortKey3 = Convert.ToInt16(key3);
        return (((ulong)key1 & 0xffffffff) | (((ulong)shortKey2 & 0xffff) << 32) | (((ulong)shortKey3 & 0xffff) << 48));
    }


    public static ulong GetKey(int key1, int key2, int key3, int key4)
    {
        short shortKey1 = Convert.ToInt16(key1);
        short shortKey2 = Convert.ToInt16(key2);
        short shortKey3 = Convert.ToInt16(key3);
        short shortKey4 = Convert.ToInt16(key4);
        return (((ulong)shortKey1 & 0xffff) | (((ulong)shortKey2 & 0xffff) << 16) | (((ulong)shortKey3 & 0xffff) << 32) | (((ulong)shortKey4 & 0xffff) << 48));
    }

    public static ulong GetKey(int key1, int key2, int key3, int key4, int key5)
    {
        Debug.LogError("不支持5个key");
        return 0;
    }
    #endregion

    /// <summary>
    /// 获得一个音效ID
    /// </summary>
    /// <param name="id"></param>
    /// <param name="_csv_sound"></param>
    /// <returns></returns>
    public static int GetSoundId(int id, CSVSound _csv_sound)
    {
        int sound_id = 0;
        switch (id)
        {
            case 1:
                {
                    sound_id = _csv_sound.sound_id1;
                }
                break;
            case 2:
                {
                    sound_id = _csv_sound.sound_id2;
                    if (sound_id <= 0)
                    {
                        return GetSoundId(1, _csv_sound);
                    }
                }
                break;
            case 3:
                {
                    sound_id = _csv_sound.sound_id3;
                    if (sound_id <= 0)
                    {
                        return GetSoundId(2, _csv_sound);
                    }
                }
                break;
            case 4:
                {
                    sound_id = _csv_sound.sound_id4;
                    if (sound_id <= 0)
                    {
                        return GetSoundId(3, _csv_sound);
                    }
                }
                break;
            default:
                return GetSoundId(4, _csv_sound);
        }
        return sound_id;
    }
}
